package com.frostmourne.mxsattributeinventory.modules.extra;

import com.gitee.mxstars.mxscript.MXScriptEngine;
import com.gitee.mxstars.mxscript.MXScriptExtra;
import me.clip.placeholderapi.PlaceholderAPI;
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
import org.apache.commons.lang3.tuple.MutablePair;
import org.apache.commons.lang3.tuple.Pair;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public class BroadCastScript extends MXScriptExtra {

    @Override
    public Map<String, Pair<Class<?>, Method>> extraScript() {
        Class<?> scriptClass = BroadCastScript.class;
        Map<String, Pair<Class<?>,Method>> scriptList = new HashMap<>();
        Method method;
        try {
            method = scriptClass.getDeclaredMethod("sendBroadCast", CommandSender.class, String.class);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        }
        Pair<Class<?>,Method> methodPair = new MutablePair<>(scriptClass,method);
        scriptList.put("BC",methodPair);
        return scriptList;
    }

    public static void sendBroadCast(CommandSender sender,String bc){

        if (sender instanceof Player){
            bc = PlaceholderAPI.setPlaceholders((OfflinePlayer) sender,bc);
        }

        Bukkit.broadcastMessage(bc);

    }
}
